package com.zlx.sharelive.activity;

import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.zlx.module_base.base_ac.BaseAc;
import com.zlx.module_base.base_api.res_data.BaseBean;
import com.zlx.module_base.base_api.res_data.CmfIssueFeedbackBean;
import com.zlx.module_base.base_api.res_data.CmfIssueFeedbackBody;
import com.zlx.module_base.base_api.res_data.FeedbackBean;
import com.zlx.module_base.base_api.util.ApiUtil;
import com.zlx.module_network.api1.livedata.BaseObserver;
import com.zlx.module_network.api1.livedata.BaseObserverCallBack;
import com.zlx.module_network.bean.ApiResponse;
import com.zlx.sharelive.R;
import com.zlx.sharelive.adapter.CommonRecyclerViewAdapter;
import com.zlx.sharelive.adapter.CommonRecyclerViewHolder;
import com.zlx.sharelive.utils.NullTokenUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 意见反馈
 */
public class FeedbackAc extends BaseAc {

    @BindView(R.id.tv_type1)
    TextView tv_type1;
    @BindView(R.id.tv_type2)
    TextView tv_type2;
    @BindView(R.id.tv_type3)
    TextView tv_type3;
    @BindView(R.id.view_type1)
    View view_type1;
    @BindView(R.id.view_type2)
    View view_type2;
    @BindView(R.id.view_type3)
    View view_type3;
    @BindView(R.id.rv_data)
    RecyclerView rv_data;

    private List<TextView> mTextViewList = new ArrayList<>();
    private List<View> mViewList = new ArrayList<>();
    private List<CmfIssueFeedbackBean.InfoDTO.ResultDTO.RecordsDTO> mList = new ArrayList<>();
    private CommonRecyclerViewAdapter<CmfIssueFeedbackBean.InfoDTO.ResultDTO.RecordsDTO> mAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.ac_feedback;
    }

    @Override
    public void initViews() {
        super.initViews();

        cmfIssueFeedback("1");


        mTextViewList.clear();
        mTextViewList.add(tv_type1);
        mTextViewList.add(tv_type2);
        mTextViewList.add(tv_type3);
        mViewList.clear();
        mViewList.add(view_type1);
        mViewList.add(view_type2);
        mViewList.add(view_type3);

        initRecyclerView();
    }

    private void cmfIssueFeedback(String type) {
        CmfIssueFeedbackBody cmfIssueFeedbackBody = new CmfIssueFeedbackBody();
        cmfIssueFeedbackBody.setFeedbackType(type);
        cmfIssueFeedbackBody.setPageNo("1");
        cmfIssueFeedbackBody.setPageSize("10");
        ApiUtil.getHomeApi().cmfIssueFeedback(type, "1", "10").observe(this, new BaseObserver<>(new BaseObserverCallBack<ApiResponse<CmfIssueFeedbackBean>>() {
            @Override
            public void onSuccess(ApiResponse<CmfIssueFeedbackBean> data) {
                mList.clear();
                if (null != data) {
                    if (null != data.getData()) {
                        if (data.getData().getCode() == 0) {
                            if (null != data.getData().getInfo()) {
                                if (data.getData().getInfo().size() > 0) {
                                    if (null != data.getData().getInfo().get(0).getResult()) {
                                        if (null != data.getData().getInfo().get(0).getResult().getRecords()) {
                                            if(null!=data.getData().getInfo().get(0).getResult().getRecords()){
                                                if(data.getData().getInfo().get(0).getResult().getRecords().size()>0){
                                                    mList.add(data.getData().getInfo().get(0).getResult().getRecords().get(0));
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            if (null != mList) {
                                if (mList.size() > 0) {
                                    for (int i = 0; i < mList.size(); i++) {
                                        mList.get(i).setFlag(false);
                                    }
                                }
                            }

                            mAdapter.notifyDataSetChanged();
                        } else {
                            NullTokenUtils.isNullToken(data.getData().getCode(), data.getData().getMsg());
                        }
                    }
                }
            }
        }));
    }

    private void initRecyclerView() {
        rv_data.setLayoutManager(new LinearLayoutManager(this));

        mAdapter = new CommonRecyclerViewAdapter<CmfIssueFeedbackBean.InfoDTO.ResultDTO.RecordsDTO>(this, mList) {
            @Override
            public void convert(CommonRecyclerViewHolder holder, CmfIssueFeedbackBean.InfoDTO.ResultDTO.RecordsDTO entity, int position) {
                ((LinearLayout) holder.getView(R.id.ll_title)).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (entity.isFlag() == true) {
                            mList.get(position).setFlag(false);
                            ((ImageView) holder.getView(R.id.iv_image)).setImageResource(R.mipmap.shang_img1);
                            ((TextView) holder.getView(R.id.tv_text)).setVisibility(View.GONE);
                        } else {
                            mList.get(position).setFlag(true);
                            ((ImageView) holder.getView(R.id.iv_image)).setImageResource(R.mipmap.xia_img1);
                            ((TextView) holder.getView(R.id.tv_text)).setVisibility(View.VISIBLE);
                        }
                        if (null != mAdapter) {
                            mAdapter.notifyDataSetChanged();
                        }
                    }
                });
                holder.setText(R.id.tv_text, entity.getFeedbacktypeDicttext());

                holder.setText(R.id.tv_title, entity.getTitle());
            }

            @Override
            public int getLayoutViewId(int viewType) {
                return R.layout.item_feedback;
            }
        };

        rv_data.setAdapter(mAdapter);
    }


    @OnClick({R.id.iv_back, R.id.ll_type1, R.id.ll_type2, R.id.ll_type3})
    public void setOnClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.ll_type1:
                setTextViewColor(0);
                cmfIssueFeedback("1");
                break;
            case R.id.ll_type2:
                setTextViewColor(1);
                cmfIssueFeedback("2");
                break;
            case R.id.ll_type3:
                setTextViewColor(2);
                cmfIssueFeedback("3");
                break;
        }
    }

    private void setTextViewColor(int position) {
        for (int j = 0; j < mTextViewList.size(); j++) {
            if (position == j) {
                mViewList.get(position).setVisibility(View.VISIBLE);
            } else {
                mViewList.get(j).setVisibility(View.INVISIBLE);
            }
        }
    }


}
